package tuilayer;
import java.util.*;
import controlLayer.*;
import modelLayer.*;

public class PersonUI {
    private static boolean valid;
    private static int back;
    private PersonCtr personCtr;

    public PersonUI() {
        personCtr = new PersonCtr();
    }

    public void handlePerson() {
        mainMenuPerson();
    }
    
    private void mainMenuPerson() {
        boolean stop = false;
        while (!stop) {
            int choice = writeMainMenuPerson();
            switch (choice) {
                case 0:
                    stop = true;
                    break;
                case 1:
                    createPerson();
                    break;
                case 2:
                    updatePerson();
                    break;
                case 3:
                    getPerson();
                    break;
                case 4:
                    deletePerson();
                    break;
                default: System.out.println("Please enter a valid choice.");
            }
        }
    }
    
    private int writeMainMenuPerson() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Person Menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create Person");
        System.out.println("(2) Update Person");
        System.out.println("(3) Get Person");
        System.out.println("(4) Delete Person");
        System.out.println("\nType your choice:");
        
        int choice = ValidateInput.readInt("Please enter a valid choice.");
        
        if(ValidateInput.validateInt(choice))        
            return choice;
        else {
            return 999;
        }
    }
    
    private void createPerson() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the person's name:");
        String name = keyboard.nextLine();
        if(ValidateInput.isInteger(name))
            if(Integer.parseInt(name) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateName(name) || Integer.parseInt(name) == 0)
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's name:");
                name = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's address:");
        String address = keyboard.nextLine();
        valid = false;
        if(ValidateInput.isInteger(address))
            if(Integer.parseInt(address) == 0)
                return;
        while(!valid) {
            if (ValidateInput.validateAddress(address))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's address:");
                address = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's postal code:");
        String postalCode = keyboard.nextLine();
        if(ValidateInput.isInteger(postalCode))
            if(Integer.parseInt(postalCode) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validatePostalCode(postalCode))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's postal code:");
                postalCode = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's city:");
        String city = keyboard.nextLine();
        valid = false;
        if(ValidateInput.isInteger(city))
            if(Integer.parseInt(city) == 0)
                return;
        while(!valid) {
            if (ValidateInput.validateCity(city))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's city:");
                city = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's phone number:");
        String phone = keyboard.nextLine();
        if(ValidateInput.isInteger(phone))
            if(Integer.parseInt(phone) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validatePhoneNumber(phone))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's phone number:");
                phone = keyboard.nextLine();
            }
        }
        
        personCtr.createPerson(name, address, postalCode, city, phone);
        
        System.out.println("\nPerson successfully created!");
    }
    
    private void updatePerson() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the person's id:");
        int id = ValidateInput.readInt("Type the person's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the person's id:");
                id = ValidateInput.readInt("Type the person's id:");
            }
        }
        
        System.out.println("\nType the person's new name:");
        String name = keyboard.nextLine();
        if(ValidateInput.isInteger(name))
            if(Integer.parseInt(name) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateName(name))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's new name:");
                name = keyboard.nextLine();
            }
        }
    
        System.out.println("\nType the person's new address:");
        String address = keyboard.nextLine();
        if(ValidateInput.isInteger(address))
            if(Integer.parseInt(address) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateAddress(address))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's new address:");
                address = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's new postal code:");
        String postalCode = keyboard.nextLine();
        if(ValidateInput.isInteger(postalCode))
            if(Integer.parseInt(postalCode) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validatePostalCode(postalCode))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's new postal code:");
                postalCode = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's new city:");
        String city = keyboard.nextLine();
        if(ValidateInput.isInteger(city))
            if(Integer.parseInt(city) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateCity(city))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's new city:");
                city = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the person's new phone number:");
        String phone = keyboard.nextLine();
        if(ValidateInput.isInteger(phone))
            if(Integer.parseInt(phone) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validatePhoneNumber(phone))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the person's new phone number:");
                phone = keyboard.nextLine();
            }
        }
        
        personCtr.updatePerson(id, name, address, postalCode, city, phone);
        
        System.out.println("\nPerson successfully updated!");
    }
    
    private void getPerson() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the person's id:");
        int id = ValidateInput.readInt("Type the person's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the person's id:");
                id = ValidateInput.readInt("Type the person's id:");
            }
        }
        
        Person person = personCtr.getPerson(id);
        System.out.println();
        System.out.println("Name:\t\t" + person.getName());
        System.out.println("Address:\t" + person.getAddress());
        System.out.println("Postal Code:" + person.getPostalCode());
        System.out.println("City:\t\t" + person.getCity());
        System.out.println("Phone:\t\t" + person.getPhone());
    }
    
    private void deletePerson() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the person's id:");
        int id = ValidateInput.readInt("Type the person's id:");
        if(id == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateID(id))
                valid = true;
            else {
                System.out.println("\nThis id does not exist.");
                System.out.println("Type the person's id:");
                id = ValidateInput.readInt("Type the person's id:");
            }
        }
        
        personCtr.deletePerson(id);
        
        System.out.println("\nPerson successfully deleted!");
    }
}